/* Driver template for the LEMON parser generator.
** The author disclaims copyright to this source code.
*/
#ifndef _PAXER_PARSER_TEMPLATE_H_
#define _PAXER_PARSER_TEMPLATE_H_

#include <ostream>
#include <string>
#include <deque>
#include <assert.h>
#include <boost/scoped_ptr.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <caspp/caspp.h>
#include <paxer/driver.h>

#define LEMONPP_NS_BEGIN namespace Paxer {
#define LEMONPP_NS_END };

#define LEMONPP_DRIVER_TYPE Driver
#define LEMONPP_NODE_TYPE CASpp::Node

%%parser type defines%%

LEMONPP_NS_BEGIN

class Parser {

public:

    typedef LEMONPP_NODE_TYPE Node;
    typedef boost::shared_ptr<Node> NodeRef;
    typedef LEMONPP_DRIVER_TYPE Driver;
    typedef boost::shared_ptr<Driver> DriverRef;

    class Token {
    public:
        enum Values {
            %%token enum%%
            Nothing=0
        };
    };

    class Const {
    public:
        enum Values {
            %%parser const enum%%
            Nothing=0
        };
    };

    typedef LEMONPP_STATE_TYPE State;
    typedef LEMONPP_SYMBOL_TYPE Code;
    typedef LEMONPP_RULE_TYPE Rule;

    Parser(const DriverRef & driverRef);

    NodeRef nodeRef(const int index) const; /* index >= 0; from the end, < 0 from the beginning */

    void parse(const Code token_code,  /* The major token code number */
                NodeRef & nodeRef     /* The value for the token */
            );
    NodeRef endInput();

    void restart();

    int peak() const;             /* Return the peak size of the stack. */

    DriverRef driverRef() const;

#ifndef NDEBUG
    static const std::string tokenName(const Code code);
    static const std::string ruleName (const Rule code);
#endif

#ifndef NDEBUG
public:
    void activateTrace(std::ostream * out, const std::string & prompt = "");
private:
    std::ostream& log() const;
    std::ostream * log_m;
    std::string prompt_m;
#endif

private:
    class Imp;
    typedef boost::shared_ptr<Imp> ImpRef;
    ImpRef imp_m;
};

LEMONPP_NS_END

#endif// _PAXER_PARSER_TEMPLATE_H_
